package org.jeecg.modules.api.utils;

import com.alibaba.fastjson.JSON;
import com.ccqtgb.sxp.partner.security.MessageProcess;
import com.ccqtgb.sxp.partner.security.bo.Head;
import com.ccqtgb.sxp.partner.security.bo.RequestData;
import com.ccqtgb.sxp.partner.security.bo.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.properties.config.CCQTGBPropertyConfig;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Configuration
@Slf4j
public class ApiDataUtils {

    @Resource
    private CCQTGBPropertyConfig ccqtgbPropertyConfigBase;

    //当前工具类
    private static CCQTGBPropertyConfig ccqtgbPropertyConfig;

    //解决静态方法中不能直接用oaConfig的问题
    @PostConstruct
    public void init() {
        ccqtgbPropertyConfig = this.ccqtgbPropertyConfigBase;
    }

    public static <T> RequestData<T> getRequestData(String message, Class<T> tClass) {
        try {
            log.info(tClass.getName() + "三峡银行请求字符串：" + message);
            RequestData<T> dataReslt = MessageProcess.getRequestMessageCallBack(message, ccqtgbPropertyConfig.getPublicKeyPath(), ccqtgbPropertyConfig.getAesKey(), tClass);
            return dataReslt;
        } catch (Exception e) {
            throw new JeecgBootException(e.getMessage());
        }
    }

    public static <T> ResponseData<String> getResponseData(T data, Head head) {
        try {
            log.info("返回三峡银行字符串原文：" + JSON.toJSONString(data));
            ResponseData<String> messageCallBack = MessageProcess.getResponseMessageCallBack(data,
                    head.getTranchannel(), ccqtgbPropertyConfig.getPrivateKeyPath(), ccqtgbPropertyConfig.getAesKey(),
                    head, 200, "请求成功");
            log.info("返回三峡银行字符串：" + JSON.toJSONString(messageCallBack));
            return messageCallBack;
        } catch (Exception e) {
            throw new JeecgBootException("请求返回参数加密失败！");
        }
    }

}
